package com.project.easy.common.framework.captcha.core;

import com.project.easy.common.framework.captcha.core.vo.BaseCaptchaConfigVo;
import com.project.easy.common.framework.captcha.core.vo.CaptchResultVo;
import  com.project.easy.common.util.exception.captcha.CaptchaException;
import  com.project.easy.common.util.exception.sys.SysInitException;
import  com.project.easy.common.util.io.CloseResouce;
import  com.project.easy.common.util.string.StringUtil;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.Base64;
import java.util.Date;
import java.util.Objects;

/**
 * @author GreatorMan.cool
 * @copyright: Copyright (c) 2020
 * @company: 扬起科技有限公司
 * @date 2021/11/21 10:54
 * @description:
 */
public abstract class CaptchaProcessor {
    /**
     * 验证编码生产者
     * @return 返回生产图
     */
    public DefaultKaptchaProduct getProducter() {
        throw  new SysInitException("系统验证码参数配置缺失");
    }
    /**
     * 返回配置
     * @return 返回配置
     */
    public BaseCaptchaConfigVo getConfig() {
        throw  new SysInitException("系统验证码参数基础配置缺失");
    }
    /**
     * 验证码生成
     * @param captchaKey 字符Key
     * @param imageFormatName 图片格式
     * @param bizType  业务类型
     * @return 返回Base64 图
     * @throws CaptchaException 异常
     */
    public CaptchResultVo create(String captchaKey, String imageFormatName, String bizType) throws CaptchaException {
        if (StringUtil.isBlank(bizType)) {
            bizType = "login";
        }
        CaptchResultVo captchResultVo = new CaptchResultVo(bizType);
        BufferedImage bufferedImage = null;
        ByteArrayOutputStream stream = null;
        try {
            String text = getProducter().createText();
            String result = getProducter().getResult(text);
            bufferedImage = getProducter().createImage(text);
            if (StringUtil.isBlank(imageFormatName)) {
                imageFormatName = "png";
            }
            stream = new ByteArrayOutputStream();
            ImageIO.write(bufferedImage, imageFormatName, stream);
            String contentBase64 = Base64.getEncoder().encodeToString(stream.toByteArray());
            contentBase64 = contentBase64.replaceAll("\n", "").replaceAll("\r", "");
            contentBase64 = String.format("data:image/jpg;base64,%s", contentBase64);
            CaptchResultVo success = captchResultVo.success(contentBase64, captchaKey, result);
            success.setResult(this.getProducter().getResult(success.getText()));
            if (!Objects.isNull(getConfig().getTimeOut())) {
                Date date = new Date();
                success.setExpireTime(date);
            }
            this.persistence(success);
            return success;
        } catch (Exception e) {
            throw  new CaptchaException(e);
        } finally {
            CloseResouce.close(bufferedImage);
            CloseResouce.close(stream);
        }
    }
    /**
     * 验证编码持久化
     * @param captchResultVo  结果Vo
     * @return true 持久化成功 false 失败
     */
    public abstract Boolean persistence(CaptchResultVo captchResultVo);
    /**
     * 验证编码实现
     * @param captchaKey 编码
     * @param text 内容
     * @return 返回是正确
     */
    public Boolean validata(String captchaKey, String text) {
        return Boolean.TRUE;
    }
    /**
     * 验证频率
     * @param captchaKey 验证=KEY
     */
    public  void validataRate(String captchaKey) {
    }
}
